package com.zhang.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zhang.system.dto.RoleSearchDTO;
import com.zhang.system.entity.Role;
import com.zhang.system.mapper.RoleMapper;
import com.zhang.system.service.IRoleService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 创世神
 * @since 2023-07-03
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {


    @Override
    public boolean isAdmin(List<Long> roleIds) {
        List<Role> roles = listByIds(roleIds);
        List<String> collect = roles.stream().map(Role::getRoleKey).collect(Collectors.toList());
        /*如果是超级管理员*/
        return collect.contains("admin");
    }

    @Override
    public boolean isAdmin(Long roleId) {
        Role byId = getById(roleId);
        return "admin".equals(byId.getRoleKey());
    }

    @Override
    public Page<Role> searchRoles(RoleSearchDTO roleSearchDTO) {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        String searchValue = roleSearchDTO.getSearchValue();
        if (!StrUtil.isEmpty(searchValue)){
            wrapper.or().eq(Role::getRoleKey,searchValue).or().eq(Role::getRoleName,searchValue)
                    .or().eq(Role::getId,searchValue);
        }
        if (roleSearchDTO.getState()!=-1){
            wrapper.eq(Role::getState,roleSearchDTO.getState());
        }
        return page(new Page<>(roleSearchDTO.getPageNum(), roleSearchDTO.getPageSize()), wrapper);
    }
}
